package com.sc.dao.impl;

import com.sc.dao.CartItemDao;
import com.sc.entity.CartItem;
import com.sc.utils.JDBCUtils;

import java.util.List;

public class CartItemDaoImpl implements CartItemDao {
public CartItemDaoImpl(){
}

    @Override
    public int add(CartItem cartItem) throws Exception {
        String sql = "insert into cart_item (cart_id,product_id,product_num,price)" +
                "values(?,?,?,?)";
        return JDBCUtils.update(sql,new Object[]{cartItem.getCartId(),cartItem.getProductId()
        ,cartItem.getProductNum(),cartItem.getPrice()});
    }

    @Override
    public int delete(int id) throws Exception {
    String sql = "delete from cart_item where id = ?";
        return JDBCUtils.update(sql,new Object[]{id});
    }

    @Override
    public int update(CartItem cartItem) throws Exception {
        String sql = "update cart_item set cart_id = ?,product_id = ?,product_num =?" +
                ",price = ?";
        return JDBCUtils.update(sql,new Object[]{cartItem.getCartId(),cartItem.getProductId(),
        cartItem.getProductNum(),cartItem.getPrice()});
    }

    @Override
    public CartItem queryById(int id) throws Exception {
        String sql = "select from cart_item where id = ?";
        return JDBCUtils.query(sql,CartItem.class,new Object[]{id});
    }

    @Override
    public List<CartItem> queryAll() throws Exception {
        String sql = "select from cart_item ";
        return JDBCUtils.querylist(sql, CartItem.class,new Object[]{0});

    }

    @Override
    public CartItem finByCartIdAndProductId(Integer cartId, Integer productId) throws Exception {
    String sql="select * from cart_item where cart_id =? and product_id=?";
        return JDBCUtils.query(sql,CartItem.class,new Object[]{cartId,productId});
    }

    @Override
    public int deleteByCartIdAndProductId(Integer cartId, Integer productId) throws Exception {
        String sql="delete from cart_item where cart_id =? and product_id=?";
        return JDBCUtils.update(sql,new Object[]{cartId,productId});
    }
}
